package com.example.a79273.shandianban.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.config.NetconfigYouHuo;
import com.example.a79273.shandianban.db.ShopingCar;
import com.example.a79273.shandianban.model.YouHuoProducBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by lenovo on 2016/9/1.
 */
public class ShopCarBaseAdapter extends BaseAdapter {
    private List<ShopingCar> mData;
    private Context mContext;
    private View.OnClickListener mListener;
    private CompoundButton.OnCheckedChangeListener mChangeListener;
    private RequestParams mParams;
    private String substring;

    public ShopCarBaseAdapter(List<ShopingCar> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        this.mListener = (View.OnClickListener) mContext;
        mChangeListener = (CompoundButton.OnCheckedChangeListener) mContext;
    }

    @Override
    public int getCount() {
        return mData == null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.shopingcar_item,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        ShopingCar shopingCar = mData.get(position);
        holder.mTextViewColor.setText(shopingCar.getProductColor());//颜色
        holder.mTextViewCount.setText(shopingCar.getProductCount());//数量
        holder.mTextViewSize.setText(shopingCar.getProductSize());//尺码

        mParams = new RequestParams(shopingCar.getProductPath());
        x.http().get(mParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if( !TextUtils.isEmpty(result)) {
                    Type listType = new TypeToken<LinkedList<YouHuoProducBean>>(){}.getType();
                    Gson gson = new Gson();
                    LinkedList<YouHuoProducBean> resources = gson.fromJson(result, listType);
                    String pic = resources.get(0).getData().getMallDetails().getPic();
                    String name = resources.get(0).getData().getMallDetails().getName();
                    String price = resources.get(0).getData().getMallDetails().getPrice();
                    Picasso.with(mContext).load(NetconfigYouHuo.PIN_JIE+pic).into(holder.mImageViewImg);//设置图片
                    holder.mTextViewName.setText(name);
                    substring = price.substring(0, price.indexOf(".") + 3);
                    holder.mTextViewPrice.setText(substring);
                    holder.mCheckBoxChoose.setOnCheckedChangeListener(mChangeListener);
                    holder.mCheckBoxChoose.setTag(R.id.cb_shopcar_item_choose,position);
                    holder.mCheckBoxChoose.setTag(R.id.cb_shopingcar_chooseall,substring);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(mContext, "网络出小差啦...", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });

        holder.mImageViewAdd.setOnClickListener(mListener);
        holder.mImageViewAdd.setTag(position);
        holder.mImageViewSub.setOnClickListener(mListener);
        holder.mImageViewSub.setTag(position);
        return convertView;
    }

    static class ViewHolder{
        private CheckBox mCheckBoxChoose;
        private TextView mTextViewName,mTextViewColor,mTextViewSize,mTextViewPrice,mTextViewCount;
        private ImageView mImageViewImg,mImageViewAdd,mImageViewSub;
        public ViewHolder(View view) {
            mCheckBoxChoose = (CheckBox) view.findViewById(R.id.cb_shopcar_item_choose);
            mTextViewName = (TextView) view.findViewById(R.id.tv_shopcar_item_name);
            mTextViewColor = (TextView) view.findViewById(R.id.tv_shopcar_item_color);
            mTextViewSize = (TextView) view.findViewById(R.id.tv_shopcar_item_size);
            mTextViewPrice = (TextView) view.findViewById(R.id.tv_shopcar_item_price);
            mTextViewCount = (TextView) view.findViewById(R.id.tv_shopcar_item_count);
            mImageViewImg = (ImageView) view.findViewById(R.id.iv_shopcar_item_img);
            mImageViewAdd = (ImageView) view.findViewById(R.id.iv_shopcar_item_add);
            mImageViewSub = (ImageView) view.findViewById(R.id.iv_shopcar_item_sub);

        }
    }


}
